package com.crane.oss.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crane.common.core.exception.AppException;
import com.crane.common.core.exception.BasicErrorCode;
import com.crane.common.core.exception.SysException;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.app.bean.req.AppUnBindingStudentReq;
import com.crane.oss.school.bean.request.QueryStudentUserReq;
import com.crane.oss.school.bean.request.studentuser.AddStudentUserReq;
import com.crane.oss.school.bean.request.studentuser.UpdateStudentUserReq;
import com.crane.oss.school.bean.resp.StudentUserResp;
import com.crane.oss.school.dao.StudentUserDao;
import com.crane.oss.school.model.StudentUser;
import com.crane.oss.school.service.StudentUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author zhenxing.pi
 * @email ccsu123456@qq.com
 * @date 2022年04月16日 19时52分58秒
 */
@Service
@Slf4j
public class StudentUserServiceImpl implements StudentUserService {

    @Resource
    protected StudentUserDao studentUserDao;

    @Override
    public StudentUserResp queryStudentUser(Long id) {
        log.info("StudentUserBOImpl queryStudentUser id={}", id);
        StudentUser studentUser = studentUserDao.selectById(id);
        if (null == studentUser) {
            throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "studentUser");
        }
        StudentUserResp studentUserResp = new StudentUserResp();
        BeanUtil.copyProperties(studentUser, studentUserResp);
        return studentUserResp;
    }

    @Override
    public IPage<StudentUserResp> queryList(QueryStudentUserReq queryRequest) {
        log.info("StudentUserBOImpl queryList queryRequest={}", queryRequest);
        IPage<StudentUserResp> result = new Page<>();
        result.setCurrent(queryRequest.getPageNo());
        result.setPages(queryRequest.getPageSize());

        LambdaQueryWrapper<StudentUser> queryStudentUserWrapper = new LambdaQueryWrapper<>();
        IPage<StudentUser> page = studentUserDao
                .selectPage(new Page<>(queryRequest.getPageNo(), queryRequest.getPageSize()),
                        queryStudentUserWrapper);
        return page.convert(u -> {
            StudentUserResp v = new StudentUserResp();
            BeanUtils.copyProperties(u, v);
            return v;
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(AddStudentUserReq addRequest) {
        log.info("StudentUserBOImpl save request={}", addRequest);
        StudentUser studentUser = new StudentUser();
        BeanUtil.copyProperties(addRequest, studentUser);
        int i = 0;
        try {
            i = studentUserDao.insert(studentUser);
        } catch (Exception e) {
            log.info("StudentUserBOImpl add e={}", e);
            throw new SysException();
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(UpdateStudentUserReq updateRequest) {
        log.info("StudentUserServiceImpl update request={}", updateRequest);
        StudentUser studentUser = new StudentUser();
        BeanUtil.copyProperties(updateRequest, studentUser);
        int i = 0;
        try {
            i = studentUserDao.updateById(studentUser);
        } catch (Exception e) {
            log.info("StudentUserServiceImpl add e={}", e);
            throw new SysException();
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long id) {
        log.info("StudentUserServiceImpl delete id={}", id);
        return studentUserDao.deleteById(id);
    }

    @Override
    public List<StudentUserResp> queryStudentInfo(Long appUserId) {
        LambdaQueryWrapper<StudentUser> queryStudentUserWrapper = new LambdaQueryWrapper<>();
        queryStudentUserWrapper.eq(StudentUser::getAppUserId, appUserId).eq(StudentUser::getDelFlag, 1);
        List<StudentUser> studentUsers = studentUserDao.selectList(queryStudentUserWrapper);
        if (studentUsers == null) {
            return null;
        }
        List<StudentUserResp> list = BeanUtil.copyList(studentUsers, StudentUserResp.class);
        return list;
    }

    @Override
    public Integer countStudentId(Long studentId, Long userId) {
        Integer count = studentUserDao
                .selectCount(
                        new LambdaQueryWrapper<StudentUser>().eq(StudentUser::getStudentId, studentId).eq(
                                Objects.nonNull(userId), StudentUser::getAppUserId, userId));
        return count;
    }

    @Override
    public void deleteStudent(AppUnBindingStudentReq appBindingStudentReq) {
        studentUserDao.delete(new LambdaQueryWrapper<StudentUser>()
                .eq(StudentUser::getAppUserId, appBindingStudentReq.getId())
                .eq(StudentUser::getStudentId, appBindingStudentReq.getStudentId()));
    }

}
